package com.huitone.smspfm.oldclient.store;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.huitone.smspfm.oldclient.app.AppStaticConfigs;
import com.huitone.smspfm.oldclient.logger.ExtLogger;

import com.bin.common.utils.FileUtils;
import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.oldclient.model.MtGroup;

public class MtGroupTmpStore implements Serializable {
	
	private final static ExtLogger logger = ExtLogger.create(MtGroupTmpStore.class);

	private static final long serialVersionUID = 1L;
	
	private static Map<String, MtGroup> store = new ConcurrentHashMap<>();
	
	public synchronized static void add(MtGroup mtGroup) {
		String groupKey = mtGroup.getGroupKey();
		if (store.containsKey(groupKey)) {
			store.get(groupKey).groupMerge(mtGroup);
		} else {
			store.put(groupKey, mtGroup);
		}
	}
	
	public synchronized static void remove(String groupKey) {
		if (groupKey == null) {
			return;
		}
		if (store.containsKey(groupKey)) {
			store.remove(groupKey);
		}
	}
	
	public static Map<String, MtGroup> get() {
		return store;
	}
	
	public static void lmtSmsSerialize() {
		BufferedOutputStream bos = null;
		ObjectOutputStream oos = null;
		try {
			
			File file = null;
			if (!StringUtils.isEmpty(AppStaticConfigs.mtGroupTmpStoreFile)) {
				try {
					file = FileUtils.createFile(AppStaticConfigs.mtGroupTmpStoreFile);
				} catch (Exception e) {
					logger.error("创建文件失败,将使用默认路径下的MtGroupTmpStore文件", e);
				}
			}
			
			if (file == null) {
//				file = FileUtils.createFile(FileUtils.getJarParentPath(MtGroupTmpStore.class)+"/store/lmtstore.dat");
				file = FileUtils.createFile(FileUtils.getJarParentPath(MtGroupTmpStore.class)+File.separator +"store"+File.separator+"lmtstore.dat");
			}
			
		    bos = new BufferedOutputStream(new FileOutputStream(file));
		    oos = new ObjectOutputStream(bos);
		    oos.writeObject(store);
		    oos.flush();
		    oos.close();
		    logger.info("write object success! storeSize:" + store.size());
		} catch (IOException e) {
		    logger.error("write object failed!", e);
		} finally {
			if (oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					logger.error(e);
				}
			}
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					logger.error(e);
				}
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void lmtSmsDeSerialize() {
		
		BufferedInputStream bis = null;
		ObjectInputStream ois = null;
		try {
			
			File file = null;
			if (!StringUtils.isEmpty(AppStaticConfigs.mtGroupTmpStoreFile)) {
				try {
					file = FileUtils.createFile(AppStaticConfigs.mtGroupTmpStoreFile);
				} catch (Exception e) {
					logger.error("创建文件失败,将使用默认路径下的MtGroupTmpStore文件", e);
				}
			}
			
			if (file == null) {
//				file = FileUtils.createFile(FileUtils.getJarParentPath(MtGroupTmpStore.class)+"/store/lmtstore.dat");
				file = FileUtils.createFile(FileUtils.getJarParentPath(MtGroupTmpStore.class)+File.separator +"store"+File.separator+"lmtstore.dat");
			}
			
			bis = new BufferedInputStream(new FileInputStream(file));
			ois = new ObjectInputStream(bis);
			store =  (Map<String, MtGroup>) ois.readObject();
		    logger.info("read object success! storeSize:" + store.size());
		} catch (EOFException e) {} catch (IOException | ClassNotFoundException e) {
		    logger.error("read object failed!", e);
		} finally {
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					logger.error(e);
				}
			}
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					logger.error(e);
				}
			}
		}
	}
}
